package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.RemindDao;
import com.gdit.zax.entitys.Remind;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

public class addremind extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		String theme = request.getParameter("theme");
		String plan = request.getParameter("plan");
		String time = request.getParameter("time");
		String repear = request.getParameter("repear");
		int ring = TextUtil.String2int(request.getParameter("ring"), -1);
		int vibrator = TextUtil.String2int(request.getParameter("vibrator"), -1);
		
		//检测参数
		if (TextUtil.isEmpty(accesstoken)) {
			O.printFail(response, 1, "请检查accesstoken参数!");
			return;
		}
		
		if (TextUtil.isEmpty(theme)) {
			O.printFail(response, 1, "请检查theme参数!");
			return;
		}
		
		if (TextUtil.isEmpty(plan)) {
			O.printFail(response, 1, "请检查plan参数!");
			return;
		}
		
		if (TextUtil.isEmpty(time)) {
			O.printFail(response, 1, "请检查time参数!");
			return;
		}
		
		if (TextUtil.isEmpty(repear)) {
			O.printFail(response, 1, "请检查repear参数!");
			return;
		}
		
		if (ring<0) {
			O.printFail(response, 1, "请检查ring参数!");
			return;
		}
		
		if (vibrator<0) {
			O.printFail(response, 1, "请检查vibrator参数!");
			return;
		}
		
		//检测连接、用户
		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
		if (uid==Integer.MIN_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		Remind remind = new Remind();
		remind.setPlan(plan);
		remind.setRepear(repear);
		remind.setRing(ring);
		remind.setTheme(theme);
		remind.setTime(time);
		remind.setUserid(uid);
		remind.setVibrator(vibrator);
		
		
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		RemindDao rdao = dao.getRemindDao();
		
 		if (rdao.addRemind(remind)){
 			O.printSuccess(response, "添加成功!");
 		} else {
 			O.printFail(response, 1000, "系统出错！");
 		}
		
	}

}
